package com.demo.chapter11future;

import java.util.concurrent.CompletableFuture;

//组合多个CompletableFuture的结果
public class ThenCombine {

	public static Integer calc(Integer para){
		try{
			//模拟一个长时间执行
			Thread.sleep(1000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		return para / 2;
	}

	public static void main(String[] args) throws Exception {
		CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> calc(50));
		CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> calc(25));
		final CompletableFuture<Void> future3 = future1
			.thenCombine(future2, (i, j) -> i + j)
			.thenApply(i -> Integer.toString(i))
			.thenApply(str -> "\"" + str + "\"")
			.thenAccept(System.out::println);
		future3.get();
	}

}
